<style>
	tr.sub_row{background:#CCC;}
	tr.sub_row td{padding-left:20px;}
</style>
<div style="padding-bottom:20px;" class="button_section">
	<!--<form>
    	<input type="text"><input type="submit" value="search">
    </form>-->
        <div class="left width65p componentheading"><?php echo $page_title;?></div>
        <a class="button" href="<?php echo site_url('category/add')?>">Add New</a>
        <div class="clear"></div>
</div>
<div class="box-header">Lists</div>
<table width="100%">
	<thead>
      <tr>
            <th width="37%" height="32">Thematic Area</th>
            <th width="33%">Code</th>
            <th width="30%">Options</th>
      </tr>
   </thead>
   <tbody>
      <?php if(count($categories) > 0): ?>
      <?php foreach($categories as $category) : ?>
            <tr>
            	<td><?php echo $category->category_name;?></td>
                <td><?php echo $category->code;?></td>
                <td><a href="<?php echo site_url('category/edit/'.$category->id)?>">Edit</a>
                    <?php if(is_super_admin()) : ?>
                &nbsp;|&nbsp;<a href="javascript:;" onClick="delete_category(<?php echo $category->id;?>,true);">Delete</a> <?php endif; ?></td>
            </tr>
            
            <?php $sub_categories = $this->general_db_model->getAll('tbl_publication_category', 'id', 'parent_id = '.$category->id);?>
            <?php if(count($sub_categories) > 0) :
						foreach($sub_categories as $sub_cat):
			?>
            				<tr class="sub_row">
                            	<td><?php echo $sub_cat->category_name;?></td>
                                <td><?php echo $category->code."".$sub_cat->code;?></td>
                                <td style="text-align:left;"><a href="<?php echo site_url('category/edit/'.$sub_cat->id)?>">Edit</a>
                                    <?php if(is_super_admin()) : ?>
                                    &nbsp;|&nbsp;<a href="javascript:;" onClick="delete_category(<?php echo $sub_cat->id;?>);">Delete</a> <?php endif; ?></td>
                            </tr>		
      		<?php endforeach; endif; ?>
      <?php endforeach; ?>
      <?php else: ?>
      	<tr>
        	<td colspan="3">
      		<div class="error_message">
            	No thematic areas have been added.
            </div>
            </td>
        </tr>
      <?php endif; ?>
    </tbody>  
</table>

<script>
	function delete_category(cid, p_category)
	{ 
		if(confirm('Are You Sure???'))	
		{
			$.post( '<?php echo site_url('category/delete_category');?>', {c_id : cid, p_cat: p_category}, function(data){ window.location = '<?php echo current_url();?>';});	
		}
	}
</script>